package com.example.tourstor.myself;

import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.tourstor.R;
import com.zaaach.citypicker.CityPicker;
import com.zaaach.citypicker.adapter.OnPickListener;
import com.zaaach.citypicker.model.City;
import com.zaaach.citypicker.model.HotCity;
import com.zaaach.citypicker.model.LocateState;
import com.zaaach.citypicker.model.LocatedCity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class DealInformationActivity extends AppCompatActivity implements MyOneLineView.OnRootClickListener, MyOneLineView.OnArrowClickListener {

    private Toolbar deal_toolbar;
    private LinearLayout deal_root;

    private DatePickerDialog.OnDateSetListener onDateSetListener;
    private Calendar nowdate;
    private int mYear,mMonth,mDay;
    String birthDate = "";

    private String[] sexArry = new String[]{"女", "男"};// 性别选择
    String sex = "";

    MyOneLineView sexLineview;
    MyOneLineView dateLineview;
    MyOneLineView cityLineview;

    String currentTV = "";
    private List<HotCity> hotCities;
    private int anim = R.style.DefaultCityPickerAnimation;//默认主题
    private boolean enable = true;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deal_information);

        hotCities = new ArrayList<>();
        hotCities.add(new HotCity("北京", "北京", "101010100"));
        hotCities.add(new HotCity("上海", "上海", "101020100"));
        hotCities.add(new HotCity("广州", "广东", "101280101"));
        hotCities.add(new HotCity("深圳", "广东", "101280601"));
        hotCities.add(new HotCity("杭州", "浙江", "101210101"));

        setToolBar();
        initView();

    }

    private void setToolBar() {
        deal_toolbar = (Toolbar) findViewById(R.id.deal_toolbar);

        deal_toolbar.setTitle("个人资料");
        deal_toolbar.setTitleTextColor(Color.WHITE);

        this.setSupportActionBar(deal_toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true);//设置返回键可用
        this.getSupportActionBar().setDisplayShowTitleEnabled(true);
    }

    //Toolbar的事件---返回
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    public void initView(){

        deal_root = (LinearLayout) findViewById(R.id.deal_root);

        nowdate = Calendar.getInstance();
        mYear = nowdate.get(Calendar.YEAR);
        mMonth = nowdate.get(Calendar.MONTH);
        mDay = nowdate.get(Calendar.DAY_OF_MONTH);

        onDateSetListener = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                int mYear = year;
                int mMonth = monthOfYear;
                int mDay = dayOfMonth;
                birthDate = new StringBuffer().append(mYear).append("年").append(mMonth).append("月").append(mDay).append("日").toString();
                dateLineview.setRightText(birthDate);
            }
        };

        //使用示例，通过Java代码来创建MyOnelineView
        //icon + 文字 + 箭头
        deal_root.addView(new MyOneLineView(this)
                .initNoIcon("头像", "图片", true)
                /*.setDividerTopColor(R.color.head_bottom_color)*/
                .showDivider(true,true)
                .setDividerTopHigiht(10)
                .setOnRootClickListener(this, 1));

        deal_root.addView(new MyOneLineView(this)
                .initNoIcon("昵称", "Briain", true)
                .setOnRootClickListener(this, 2));

        sexLineview = new MyOneLineView(this);
        deal_root.addView(sexLineview
                .initNoIcon("性别", "", true)
                .setOnRootClickListener(this, 3));
       /* sexLineview.setTextContent("性别").showLeftIcon(false).showEdit(false).setRightText("")
                .showArrow(true).setOnRootClickListener(this, 3);
        deal_root.addView(sexLineview);*/


        dateLineview = new MyOneLineView(this);
        deal_root.addView(dateLineview
                .initNoIcon("出生日期", "", true)
                .setOnRootClickListener(this, 4));

        cityLineview = new MyOneLineView(this);
        deal_root.addView(cityLineview
                .initNoIcon("地区", "", true)
                .setOnRootClickListener(this, 5));

    }

    @Override
    public void onRootClick(View view) {

        switch ((int) view.getTag()) {
            case 1:
                Toast.makeText(this, "111", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(this, "222", Toast.LENGTH_SHORT).show();
                break;
            case 3:
                //Toast.makeText(this, "333", Toast.LENGTH_SHORT).show();
                showSexChooseDialog();
                break;
            case 4:
                //Toast.makeText(this, "444", Toast.LENGTH_SHORT).show();
                new DatePickerDialog(DealInformationActivity.this, onDateSetListener, mYear, mMonth, mDay).show();
                break;
            case 5:
                //Toast.makeText(this, "555", Toast.LENGTH_SHORT).show();
                CityPicker.getInstance()
                        .setFragmentManager(getSupportFragmentManager())  //此方法必须调用
                        .enableAnimation(enable)  //启用动画效果
                        .setAnimationStyle(anim)  //自定义动画
                        .setLocatedCity(null)  //APP自身已定位的城市，默认为null（定位失败）
                        .setHotCities(hotCities)  //指定热门城市
                        .setOnPickListener(new OnPickListener() {
                            @Override
                            public void onPick(int position, City data) {
                                //currentTV = (data == null ? "杭州" : String.format("当前城市：%s，%s", data.getName(), data.getCode()));
                                cityLineview.setRightText(data.getName());
                                if (data != null) {
                                    Toast.makeText(
                                            getApplicationContext(),
                                            String.format("点击的数据：%s，%s", data.getName(), data.getCode()),
                                            Toast.LENGTH_SHORT)
                                            .show();
                                }
                            }

                            @Override
                            public void onLocate() {
                                //开始定位，这里模拟一下定位
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        CityPicker.getInstance().locateComplete(new LocatedCity("深圳", "广东", "101280601"), LocateState.SUCCESS);
                                    }
                                }, 3000);
                            }
                        })
                        .show();
                break;
        }


    }

    @Override
    public void onArrowClick(View view) {

    }



    private void showSexChooseDialog() {
        AlertDialog.Builder builder3 = new AlertDialog.Builder(this);// 自定义对话框
        builder3.setSingleChoiceItems(sexArry, 0, new DialogInterface.OnClickListener() {// 2默认的选中

            @Override
            public void onClick(DialogInterface dialog, int which) {// which是被选中的位置
                // showToast(which+"");
                sexLineview.setRightText(sexArry[which]);
                dialog.dismiss();// 随便点击一个item消失对话框，不用点击确认取消
            }
        });
        builder3.show();// 让弹出框显示
    }
}